package com.smartcruise.start.loginActivity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

import com.smartcruise.R;
import com.smartcruise.base.BaseActivity;
import com.smartcruise.base.FragmentActivitySimpleImp;
import com.smartcruise.main.MainActivity;
import com.smartcruise.start.checkVerifyCode.VerifyFragment;
import com.smartcruise.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by FengChaoQun
 * on 2018/1/18 0018
 * 登录界面
 */

public class LoginActivity extends BaseActivity implements LoginContract.View {

    @BindView(R.id.account)
    EditText account;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.login)
    Button login;
    private LoginContract.Presenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setEnableRightSlide(false);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        presenter = new LoginPresenterImp(this);
        presenter.start();

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtils.isNotEmpty(account, password)) {
                    setButtonAlpha(false);
                } else {
                    setButtonAlpha(true);
                }
            }
        };

        account.addTextChangedListener(textWatcher);
        password.addTextChangedListener(textWatcher);
    }

    @Override
    public String getAccount() {
        return account.getText().toString();
    }

    @Override
    public String getPassword() {
        return password.getText().toString();
    }

    @Override
    public void setAccount(String accountText) {
        account.setText(accountText);
    }

    @Override
    public void setPassword(String passwordText) {
        password.setText(passwordText);
    }

    @Override
    public void setButtonAlpha(boolean isAlpha) {
        if (isAlpha) {
            login.setAlpha(0.5f);
        } else {
            login.setAlpha(1f);
        }
    }

    @Override
    public void gotoMainActivity() {
        MainActivity.start(getActivity());
        finish();
    }

    @OnClick(R.id.login)
    public void onViewClicked() {
        presenter.login();
    }

    @OnClick(R.id.findPassword)
    public void onFindPasswordClicked() {
        FragmentActivitySimpleImp.start(getActivity(), VerifyFragment.class);
    }
}
